package org.yoki.edu.bg.radius.filter.app.operator;

import org.yoki.edu.bg.radius.core.client.domain.Client;
import org.yoki.edu.bg.radius.filter.app.obs.SynObservable;
import org.yoki.edu.bg.radius.filter.app.receiver.packet.RadiusPacket;
import com.yunzhitx.cloud.common.protocol.radius.msg.packet.RadiusMsg;
import com.yunzhitx.cloud.common.protocol.radius.msg.parser.RadiusPacketParser;

import java.net.DatagramPacket;
import java.util.Arrays;

/**
 * @author Sky$
 * @Description: TODO
 * @date 2018/1/4$ 16:09$
 */
public class CheckPacketOperator implements SynPacketOperator {

    @Override
    public void update(SynObservable o, Object... args) {
        if(true){
            return;
        }
        if (null == args && args.length <= 0) {
            return;
        }
        Long startTime = System.currentTimeMillis();
        Long tempTime = startTime;
        RadiusPacket clientRadiusPacket = (RadiusPacket) args[0];
        DatagramPacket clientPacket = clientRadiusPacket.getPacket();
        RadiusPacket serverRadiusPacket = null;
        DatagramPacket serverPacket = null;
        if (args.length == 2) {
            serverRadiusPacket = (RadiusPacket) args[1];
            serverPacket = serverRadiusPacket.getPacket();
        }
        if (null == clientPacket) {
            return;
        }
        if (null == serverRadiusPacket || null == serverPacket) {
            System.out.println("Client packet Operate start !!!");
            opraterClientPacket(clientPacket);
        } else {
            System.out.println("Server packet Operate start !!!");
            opraterServerPacket(serverPacket);
        }
        System.out.println("Total consume " + (System.currentTimeMillis() - startTime) + " ms");

    }

    private void opraterClientPacket(DatagramPacket packet) {
        Long tempTime = System.currentTimeMillis();
        byte[] bytes = new byte[packet.getLength()];
        System.out.println("------------------------------ 获取数据包IP start ------------------------------");
        String ip = packet.getAddress().getHostAddress();
        System.out.println("Ip :  " + ip);
        System.out.println("Consume " + (System.currentTimeMillis() - tempTime) + " ms");
        tempTime = System.currentTimeMillis();
        System.out.println("------------------------------ 校验IP合法性 start ------------------------------");
        Client entity = Client.selectDetailByIpAddr(ip);
        if (null == entity) {
            System.out.println("Ip不合法，拒绝接入请求!!!");
            return;
        }
        String secret = entity.getSecret();
        System.out.println("Ip合法，密钥 : " + secret);
        System.out.println("Consume " + (System.currentTimeMillis() - tempTime) + " ms");
        tempTime = System.currentTimeMillis();
        //去除UDP数据包中只包含有效数据的一部分
        System.out.println("------------------------------ 获取UDP数据包有效数据 start ------------------------------");
        System.arraycopy(packet.getData(), 0, bytes, 0, bytes.length);
        System.out.println("Consume " + (System.currentTimeMillis() - tempTime) + " ms");
        tempTime = System.currentTimeMillis();
        System.out.println("------------------------------ Raw byte start ------------------------------");
        System.out.println(Arrays.toString(bytes));
        System.out.println("Consume " + (System.currentTimeMillis() - tempTime) + " ms");
        tempTime = System.currentTimeMillis();
        System.out.println("------------------------------ RadiusMsg start ------------------------------");
        RadiusMsg msg = RadiusPacketParser.parseReqPacket(bytes, secret);
        System.out.println("RadiusMsg  " + msg + " ms");
        System.out.println("Consume " + (System.currentTimeMillis() - tempTime) + " ms");
        tempTime = System.currentTimeMillis();
        msg.secret = "888" ;
        System.out.println("------------------------------ secret : " +  msg.secret + " , start ------------------------------");
        byte[] b1 = msg.getBytes() ;
        System.out.println(Arrays.toString(b1));
        System.out.println(RadiusPacketParser.parseReqPacket(b1, msg.secret));
        System.out.println("Consume " + (System.currentTimeMillis() - tempTime) + " ms");
        System.out.println("------------------------------ secret : " +  msg.secret + " , start ------------------------------");
        msg.secret = secret ;
        byte[] b2 = msg.getBytes() ;
        System.out.println(Arrays.toString(b2));
        System.out.println(RadiusPacketParser.parseReqPacket(b2, msg.secret));
        System.out.println("Consume " + (System.currentTimeMillis() - tempTime) + " ms");
    }

    private void opraterServerPacket(DatagramPacket packet) {

    }

}
